/*
 * cafe-route.js
 * Created by PanRS on 2017年5月26日 下午3:01:08. 
 * Copyright (C) 2017 NRIET
 * Description: 
 * 		路由跳转
 */

;define(function(undefined) {
	
	var win = window;
	
	function sidebarEves(){
		var sidebar = $('div.cafe-sidebar'),
			container = $('div.cafe-container'),
			sidswitch = $('div.cafe-sidebar-switch');
		var $font = $('i.cafe-ifont');
		sidebar.mouseover(function(){
			if(sidebar.data('hidfunc')){
				sidswitch.data('hidfunc', 'true').attr('data-hidfunc', 'true');
			}
		}).mouseout(function(){
			sidswitch.removeAttr('data-hidfunc');
		}).undelegate('click').delegate('div.cafe-sidebar-switch', 'click', function(e){
			$(this).toggleClass('on');
			var isOFF = $(this).hasClass('on')? false: true;
			var offsetv = isOFF? 15 - sidebar.width(): 0;
			$font.css({transform: 'rotate(' + (isOFF? 180: 0) + 'deg)'});
			sidebar.animate({
				left: offsetv + 'px'
			}, 100, function(){
				var w = isOFF? container.width() + Math.abs(offsetv): container.width() - sidebar.width() + 15;
				container.animate({
					width: w + 'px'
				}, 80, function(){
					setTimeout(function(){
						if($('#iContent-lout').length > 0){
							$('#iContent-lout').layout('resize');
						}
						if($('#nmap').length > 0 && GIS){
							GIS.Init.resizeMap('nmap')
						}
					}, 110);
				});
			});
		});
	}
	sidebarEves();
	
	var cfPprogress = null;
	
	//缓存菜单路径ID  index=0顶级菜单id，以此类推
	var navPathArr= [],
		$rootNav = $('ul.cafe-menu li.active');
	
	navPathArr.push($rootNav.attr('channel'));
	var CFRoute = function(setting){
		this.progress = null;
		this.storage =  CafeJS.CFStorage({type: 'session'});
		var opts = {
				clickCall: null
		};
		this.configs = $.extend({
		}, opts, setting);
		this.init();
	}
	
	CFRoute.prototype = {
			VERSION: 'V1.0',
			init: function(){
				this.eves();
			},
			eves: function(){
				var _this = this;
				
				var $iframe = $('<iframe>').attr({
						id : "loader-wrap",
						name : "loader-wrap",
						src : null,
						frameborder : 0,
						scrolling : "no",
						initMethod : "none"
				}).css({
					width : "100%",
					height : "100%",
					background : "#FFF"
				});
				////匹配外链地址表达式
				var reghttp = /^http:*|^https:*/;
				
				var RT = {
						//绑定url
						_buildUrl: function(nv){
							if(!$(nv).data('href')) return;
							var cxt = win.CafeCxtPath == 'undefined' ? '/': win.CafeCxtPath;
							return cxt + $(nv).data('href');  
						},
						//请求html
						_ajaxHtml: function(ajaxurl, z){
							var _ = this;
							//Pace.restart();
							if($("iframe#loader-wrap").length > 0) $("iframe#loader-wrap").remove();
							var $container = $("div.cafe-container"),
								target = $(z).data('target'),
								ns = $(z).data('ns'),
								href = $(z).data('href');
							$container.empty();
							if(target != '_self' && href){
			        			if(href.length > 1  && reghttp.test(href)){
			        				if(target === '_blank') {
			        					win.open(href);
			        				} else if(target === '_frame'){
			        					$container.append($iframe);
			        					$iframe.attr('src', href);
			        				}
			        			} else {
			        				ErrorUtil.cfThrow("此处配置的视图打开形式为:" + target + ",href为:" + href + ",格式不正常!请修改!");
			        			}
			        			return;
							}
							$.ajax({ 
			        			url: ajaxurl, 
			        			type: "GET",
			        			dataType: "html",
			        			crossDomain: true,
			        			async: false,
			        			data: {
			        				ns: ns,
			        			},
			        			context: $("div.cafe-container"), 
			        			success: function(rest, textStatus, jqXHR){
			        				if(jqXHR.status == 200 && textStatus == 'success'){
			        					$(this).html(rest); 
			        					if(typeof _this.configs.clickCall === 'function'){
			        						_this.configs.clickCall(z);
			        					}
			        				}
			        			},
			        			error: function (XHR, textStatus, errorThrown) {console.log(errorThrown);},
			        			complete: function(XHR, TS){}
			        		});
						},
						//回调方法
						_call: function(){
							var _ = this;
						}
				};
				//列表事件注册
				function _siderEvs(){
					$('i.cafe-sider-i').on('click', function(e){
						e.stopPropagation();
		        		RT._ajaxHtml(RT._buildUrl(this), this);
					});
				}
				_siderEvs();
				
				//顶部菜单注册事件
				$('ul.cafe-menu2').undelegate('click').delegate('li', 'click', function(e){
	        		e.stopPropagation();
	        		//Pace.restart();
	        		var ajaxurl = win.CafeCxtPath == 'undefined' ? '/' 
    						: win.CafeCxtPath + '/main/reloadLout.do';
	        		var $nv = $(this);
	        		if($nv.data('target') !== '_self') {
	        			var xurl = win.CafeCxtPath == 'undefined' ? '/' 
	    						: win.CafeCxtPath + '/main/init.do';
	        			xurl += '?rtid=' + win.EncryptUtil($nv.attr('channel'));
	        			//匹配外链系统表达式
	        			if($nv.data('href').length > 1  && reghttp.test($nv.data('href'))){
	        				xurl = $nv.data('href');
	        			}
	        			win.open(xurl);
	        		} else { 
	        			$.ajax({ 
		        			url: ajaxurl, 
		        			type: "post",
		        			dataType: "json",
		        			data: {
		        				rtid: win.EncryptUtil($nv.attr('channel')),
		        			},
		        			crossDomain: true,
		        			async: false,
		        			context: $('div.cafe-sidebar-content'),
		        			success: function(rest, textStatus, jqXHR){
		        				if(jqXHR.status == 200 && rest.success){
		        					$nv.siblings().removeClass('active');
		        					$nv.addClass('active');
		        					var data = rest.objectData;
		        					if($nv.data('href').length > 1){
		        						if(!data.sidebar) 
		        							$(this).empty()
		        						else 
		        							$(this).empty().html(data.sidebar);;
		        						var nvi = '<i data-href="' + $nv.data('href') + '"></i>'
		        						RT._ajaxHtml(RT._buildUrl(nvi));
		        					} else {
		        						$(this).empty().html(data.sidebar);
		        					}
		        					require(['layout'], function (lout){
		        						var layout = new lout.cfLayout(data.lout.sideer);
		        						sidebarEves();
		        					});
		        					CafeJS.navMenu();
		        					_siderEvs();
		        					var $actsid = $('li.i-l-ul-li');
		        					$actsid = $actsid.length > 0? $actsid.find("i") : $($('i.cafe-sider-i')[0]);
		        					$actsid.trigger('click');
		        					_this.storage.setItem('nav1', $nv.attr('channel'));
		        				}
		        			},
		        			error: function (XHR, textStatus, errorThrown) {console.log(errorThrown);},
		        			complete: function(XHR, TS){}
		        		});
	        		}
	        		return false;
	        	});
				$('ul.cafe-menu2 li.active').trigger('click');
			},
	};
	
	CafeJS.CFRouter = function(setting){
		var router = new CFRoute(setting);
		this.config = router.configs;
	}
});